package cn.ffcs.uec.wBStaffInfo.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.ForwardEvent;
import org.zkoss.zkplus.databind.BindingListModelList;
import org.zkoss.zul.ListModel;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

import cn.ffcs.raptornuke.plugin.common.zk.ctrl.BasePortletComposer;
import cn.ffcs.raptornuke.plugin.common.zk.util.ZkUtil;
import cn.ffcs.raptornuke.portal.theme.ThemeDisplay;
import cn.ffcs.uec.outStaff.manager.UecOutStaffManager;
import cn.ffcs.uec.outStaff.model.UecOutStaffInfo;
import cn.ffcs.uec.staff.manager.UecTitleManager;
import cn.ffcs.uec.staff.manager.UecTrialManager;
import cn.ffcs.uec.staff.model.UecTitle;
import cn.ffcs.uec.staff.model.UecTrial;
import cn.ffcs.uec.wBStaffInfo.action.bean.WbTitleBean;
import cn.ffcs.uom.common.constants.BaseUnitConstants;
import cn.ffcs.uom.common.util.ApplicationContextUtil;
import cn.ffcs.uom.common.util.IPortletInfoProvider;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.PlatformUtil;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.util.UomZkUtil;
import cn.ffcs.uom.common.vo.PageInfo;
import cn.ffcs.uom.common.zul.PubUtil;
import lombok.Getter;
import lombok.Setter;

/**
 * @Title:专业技术职务 CRUD插件
 */
@Controller
@Scope("prototype")
public class WbTitleMainComposer extends BasePortletComposer implements
IPortletInfoProvider {

	private static final long serialVersionUID = -5811398149406451671L;
	private String editNationdutyUrl="/pages_nmg/uecOutStaffInfomation/wb_Title_edit.zul";

	/**
	 * 人员基本信息功能 查询出的实体对象
	 */
	private UecOutStaffInfo uecStaffInfo;
	/**
	 * 对应页面上的bean
	 */
	@Getter
	private WbTitleBean bean = new WbTitleBean();
	/**
	 * 专业技术职务实体
	 */
	private UecTitle uecTitle;
	/**
	 * 翻页查询条件实体
	 */
	private UecTitle titlePaging;

	/**
	 * 专业技术职务查询实体
	 */
	// private UecAss qryUecAss;

	private List<UecTitle> titleList = new ArrayList<UecTitle>();

	/**
	 * 是否为入职页面引用
	 */
	private boolean isOutStaffEntryExt = false;

	private UecTitleManager uecTitleManager = (UecTitleManager) ApplicationContextUtil
			.getBean("uecTitleManager");
	private UecOutStaffManager uecOutStaffInfoManager = (UecOutStaffManager) ApplicationContextUtil
			.getBean("uecOutStaffManager");
	private UecTrialManager uecTrialManager = (UecTrialManager) ApplicationContextUtil
			.getBean("uecTrialManager");
	@Getter
	@Setter
	private IPortletInfoProvider portletInfoProvider;
	@Override
	public String getPortletId() {
		return super.getPortletId();
	}

	@Override
	public ThemeDisplay getThemeDisplay() {
		return super.getThemeDisplay();
	}

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		UomZkUtil.autoFitHeight(comp);
		Components.wireVariables(comp, bean);
	}


	/**
	 * 初始化
	 */
	public void onCreate$WbTitleWin() {
		onUecOutStaffTitleQueryResponse();
	}
	

	/**
	 * 选择查询记录
	 * 
	 * @throws Exception
	 */
	public void onSelectUecTitleList() throws Exception {
		if (this.bean.getUecTitleListbox().getSelectedCount() > 0) {
			uecTitle = (UecTitle) bean.getUecTitleListbox().getSelectedItem()
					.getValue();
		}
	}

	/**
	 * 人员基本信息功能，抛出事件， 事件监听
	 * 
	 * @param event
	 */
	public void onUecOutStaffTitleQueryResponse() {
		uecStaffInfo=new UecOutStaffInfo();
		if (StrUtil.isNullOrEmpty(PlatformUtil.getCurrentUser())) {
			ZkUtil.showError("当前登录用户不存在!", "错误提示");
			return;
		} else {
			String screenName = PlatformUtil.getCurrentUser().getScreenName();
			uecStaffInfo.setPsnCode(screenName.toUpperCase());
		}
		if(uecStaffInfo!=null) {
			uecStaffInfo=uecOutStaffInfoManager.findUecOutStaffInfo(uecStaffInfo);
		}
		if (null != uecStaffInfo) {
            //设置操作按钮，非盟市主管不可操作按钮
           /* if(PermissionUtil.isNoMainOutStaff()) {
                this.setButtonValid(false, true, true, true);
            }else {
                this.setButtonValid(true, true, true, false);
            }*/
			uecTitle = new UecTitle();
			uecTitle.setStaffId(uecStaffInfo.getOutStaffId());
			uecTitle.setNmStaffType(BaseUnitConstants.NM_OUT_STAFF_TYPE);
			titlePaging = uecTitle;			
			this.onQueryUecTitle();
		}
	}

	public void onCleanUecOutStaffTitleResponse(ForwardEvent event) {
		clearVariable();
		uecStaffInfo = null;
		ListboxUtils.clearListbox(this.bean.getUecTitleListbox());
		this.bean.getUecTitleListboxPaging().setTotalSize(1);
		this.setButtonValid(false, false, false, false);
	}

	/**
	 * 设置按钮
	 * 
	 * @param canAdd
	 * @param candel
	 */
	private void setButtonValid(boolean canView, boolean canAdd,
			boolean canEdit, boolean candel) {
		this.bean.getViewButton().setDisabled(!canView);
		this.bean.getAddButton().setDisabled(!canAdd);
		this.bean.getEditButton().setDisabled(!canEdit);
		this.bean.getDelButton().setDisabled(!candel);
	}

	/**
	 * 查询按钮
	 */
	public void onQueryUecTitle() {
		this.bean.getUecTitleListboxPaging().setActivePage(0);
		this.onQueryUecTitleList();
	}

	/**
	 * 重置按钮
	 */
	public void onResetUecTitle() {
		ListboxUtils.clearListbox(this.bean.getUecTitleListbox());
		this.bean.getUecTitleListboxPaging().setTotalSize(1);
	}

	/**
	 * 查看按钮
	 * 
	 * @throws Exception
	 * @throws InterruptedException
	 */
	public void onViewUecTitle() throws Exception, InterruptedException {
		if (uecTitle != null) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("opType", "view");
			map.put("uecTitle", uecTitle);
			map.put("isOutStaffEntryExt", isOutStaffEntryExt);
			Window window = (Window) Executions.createComponents(
					this.editNationdutyUrl,null,
					map);
			window.doModal();
			window.addEventListener("onOK", new EventListener() {
				@Override
				public void onEvent(Event event) throws Exception {
					if (event.getData() != null) {
						//
					}
				}
			});
		} else {
			ZkUtil.showError("请选中要查看的记录", "提示信息");
		}
	}

	/**
	 * 新增按钮
	 * 
	 * @throws Exception
	 * @throws
	 */
	public void onAddUecTitle() throws Exception {
		if (null != uecStaffInfo) {
			boolean isOnTrial = isOnTrial();
			if (!isOnTrial) {
				return;
			}
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("opType", "add");
			map.put("uecOutStaffInfo", uecStaffInfo);
			map.put("isOutStaffEntryExt", isOutStaffEntryExt);
			Window window = (Window) Executions.createComponents(
					this.editNationdutyUrl,null,
					map);
			window.doModal();
			window.addEventListener("onOK", new EventListener() {
				@Override
				public void onEvent(Event event) throws Exception {
					if (event.getData() != null) {
						// 新增或修改完成后，列表从数据库更新
						uecTitle = (UecTitle) event.getData();
						titleList.add(uecTitle);
							onQueryUecTitleList();

					}
				}
			});
		}
	}

	/**
	 * 编辑按钮
	 * 
	 * @throws Exception
	 * @throws InterruptedException
	 */
	public void onEditUecTitle() throws Exception, InterruptedException {
		if (null != uecTitle) {
			boolean isOnTrial = isOnTrial();
			if (!isOnTrial) {
				return;
			}
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("opType", "edit");
			map.put("uecTitle", uecTitle);
			map.put("isOutStaffEntryExt", isOutStaffEntryExt);
			Window window = (Window) Executions.createComponents(
					this.editNationdutyUrl,null,
					map);
			window.doModal();
			window.addEventListener("onOK", new EventListener() {
				@Override
				public void onEvent(Event event) throws Exception {
					if (event.getData() != null) {
						if (isOutStaffEntryExt) {
							titleList.remove(uecTitle);
						}
							onQueryUecTitleList();

					}
				}
			});
		} else {
			ZkUtil.showError("请选中要编辑的记录", "提示信息");
			return;
		}
	}


	/**
	 * 删除按钮
	 */
	public void onDelUecTitle() {
		if (uecTitle != null && null != uecTitle.getId()) {
			ZkUtil.showQuestion("确定要删除吗?", "提示信息", new EventListener() {
				public void onEvent(Event event) throws Exception {
					Integer result = (Integer) event.getData();
					if (result == Messagebox.OK) {
						uecTitle.removeOnly();
						PubUtil.reDisplayListbox(bean.getUecTitleListbox(),
								uecTitle, "del");
						uecTitle = null;
					}
				}
			});
		} else {
			ZkUtil.showError("请选中要删除的记录", "提示信息");
			return;
		}
	}

	/**
	 * 翻页
	 */
	public void onUecTitleListboxPaging() {
		this.onQueryUecTitleList();
	}

	/**
	 * 清除变量
	 */
	public void clearVariable() {
		uecTitle = null;
		titleList.clear();
		titlePaging = null;
	}

	/**
	 * 查询
	 */
	public void onQueryUecTitleList() {
		// 翻页查看生效数据
		if (null != titlePaging) {
			PageInfo pageInfo = uecTitleManager.queryPageInfoByUecTitle(
					titlePaging, this.bean.getUecTitleListboxPaging()
							.getActivePage() + 1, this.bean
							.getUecTitleListboxPaging().getPageSize());
			ListModel list = new BindingListModelList(pageInfo.getDataList(),
					true);
			this.bean.getUecTitleListbox().setModel(list);
			this.bean.getUecTitleListboxPaging().setTotalSize(
					pageInfo.getTotalCount());

		}
	}

	/**
	 * 判断是否入职
	 * 
	 * @return
	 */
	private boolean isOnTrial() {
		UecTrial qryUecTrial = UecTrial.newInstance();
		qryUecTrial.setStaffId(uecStaffInfo.getOutStaffId());
		qryUecTrial.setNmStaffType(BaseUnitConstants.NM_OUT_STAFF_TYPE);
		qryUecTrial = uecTrialManager.findUecTrial(qryUecTrial);
		if (null == qryUecTrial) {
			ZkUtil.showError("您未办理入职,请主管执行入职操作", "温馨提示");
			return false;
		}
		return true;
	}
}
